//
//  RYProductChoice.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/19.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit

class RYProductChoice: SYBaseView {
    
    var bgBt: UIButton = {
        let v = UIButton()
        v.backgroundColor = UIColor(white: 0, alpha: 0.5)
        return v
    }()
    
    fileprivate var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = .white
        return v
    }()

    var fir: RYProductChoiceItem = {
        let v: RYProductChoiceItem = RYProductChoiceItem()
        v.backgroundColor = UIColor(hex: 0xF4F4F4)
        return v
    }()

    var sec: RYProductChoiceItem = {
        let v: RYProductChoiceItem = RYProductChoiceItem()
        v.backgroundColor = .white
        return v
    }()
    
    var thir: RYProductChoiceItem = {
        let v: RYProductChoiceItem = RYProductChoiceItem()
        v.backgroundColor = .white
        return v
    }()
    

    override func initViews() {
        self.isHidden = true
        bgBt.addAction {[weak self] in
            if let `self` = self {
                self.isHidden = true
            }
        }
        addSubview(bgBt)
        addSubview(bgView)
        addSubview(fir)
        addSubview(sec)
        addSubview(thir)
        bgBt.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(140.autoWidth())
        }
        fir.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.height.equalTo(140.autoWidth())
            make.width.equalTo(SCREEN_WIDTH/3)
        }
        sec.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(SCREEN_WIDTH/3)
            make.top.equalToSuperview()
            make.height.equalTo(140.autoWidth())
            make.width.equalTo(SCREEN_WIDTH/3)
        }
        thir.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(SCREEN_WIDTH*2/3)
                make.top.equalToSuperview()
                make.height.equalTo(140.autoWidth())
                make.width.equalTo(SCREEN_WIDTH/3)
            }
    }
    
}


class RYProductChoiceItem: SYBaseView, UITableViewDelegate, UITableViewDataSource {
    
    var tableView: UITableView!
    var selectRow: Int?
    var datas: [[String: String]] = [] {
        didSet {
            self.tableView.reloadData()
        }
    }
    
    override func initViews() {
        setTableView()
        addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView!.register(RYProductChoiceItemCell.self, forCellReuseIdentifier: "Cell")
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: RYProductChoiceItemCell? = tableView.dequeueReusableCell(withIdentifier: "Cell") as? RYProductChoiceItemCell
        cell?.selectionStyle = .none
        cell?.titleLb.text = datas[indexPath.row]["name"]
        if let r = selectRow, indexPath.row == r {
            cell?.titleLb.textColor = UIColor(hex: 0x367FF6)
        } else {
            cell?.titleLb.textColor = UIColor(hex: 0x333333)
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("didSelectRowAt")
        selectRow = indexPath.row
        self.clickedClosure?(indexPath.row)
        self.tableView.reloadData()
    }
}


class RYProductChoiceItemCell: SYBaseCell {
    
    let titleLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 14.autoFontSize()
        return v
    }()

    
    override func initViews() {
        addSubview(titleLb)
        titleLb.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(15.autoWidth())
            make.height.equalTo(15.autoWidth())
            make.top.equalToSuperview().offset(10.autoWidth())
            make.bottom.equalToSuperview().offset(-10.autoWidth())
        }
    }
}
